package com.hebeu.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.hebeu.device.dao.*;
import com.hebeu.device.pojo.*;
import com.hebeu.device.service.DPayoffService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/*
实现Service定义的方法
 */
@Service
public class DPayoffServiceImpl implements DPayoffService {
    /*
   自动装配
    */
    @Resource
    private DPayoffMapper dPayoffMapper;
    @Resource
    private DMaintenanceMapper dMaintenanceMapper;
    @Resource
    private DPartFluidMapper dPartFluidMapper;
    @Resource
    private DPartMapper dPartMapper;
    @Resource
    private DServiceMapper dServiceMapper;

    @Override
    public void addDPayoff(Integer sId) {
        DPayoff dPayoff = new DPayoff();
        dPayoff.setServiceId(sId);
        // 从 DMaintenance数据表中调取"main_id","person_pay","mati_pay"
        DMaintenanceExample dMaintenanceExample = new DMaintenanceExample();
        DMaintenanceExample.Criteria criteria = dMaintenanceExample.createCriteria();
        //查询条件语句
        criteria.andServiceIdEqualTo(sId);
        //将查询结果放入dMaintenanceList集合中
        List<DMaintenance> dMaintenanceList = dMaintenanceMapper.selectByExample(dMaintenanceExample);
        //判断list是否为空
        if (dMaintenanceList.size() != 0) {
            //定义DMaintenance类，获取list第一条数据
            DMaintenance dMaintenance = dMaintenanceList.get(0);
            //获取DMaintenance类中的Personpay（人工费）值增加到payoff表中
            dPayoff.setPersonPay(dMaintenance.getPersonPay());
            //获取DMaintenance类中的MatiPay（材料费）值增加到payoff表中
            dPayoff.setMatiPay(dMaintenance.getMatiPay());
            //获取DMaintenance类中的注意事项增加到payoff表中
            dPayoff.setNote(dMaintenance.getNote());
            //获取DMaintenance类中的保修承若增加到payoff表中
            dPayoff.setMainPermit(dMaintenance.getMainPermit());

            //通过"main_id"查询表part_fluid中对应数据
            DPartFluidExample dPartFluidExample = new DPartFluidExample();
            DPartFluidExample.Criteria criteria1 = dPartFluidExample.createCriteria();
            //查询条件语句
            criteria1.andMainIdEqualTo(dMaintenance.getMainId());
            //将查询结果放入dPartFluidList集合中
            List<DPartFluid> dPartFluidList=dPartFluidMapper.selectByExample(dPartFluidExample);
            //判断list是否为空
            if (dPartFluidList.size()!=0){
                double sum=0.0;
                for(DPartFluid dPartFluid:dPartFluidList){
                    //通过"part_name"查询表part中对应单价
                    DPartExample dPartExample=new DPartExample();
                    DPartExample.Criteria criteria2=dPartExample.createCriteria();
                    //查询条件语句
                    criteria2.andPartNameEqualTo(dPartFluid.getPartName());
                    //将查询结果放入dPartList集合中
                    List<DPart> dPartList=dPartMapper.selectByExample(dPartExample);
                    DPart dPart=dPartList.get(0);
                    //计算维修中的成本费：单价*数量
                    sum=sum+dPart.getPrice()*dPartFluid.getNumber();
                }
                //将sum赋给payoff表中的成本
                dPayoff.setPartPay(sum);
            }

            if (dPayoff.getMatiPay()==null){
                dPayoff.setMatiPay(0.0);
            }
            if (dPayoff.getPartPay()==null){
                dPayoff.setPartPay(0.0);
            }
            if (dPayoff.getPersonPay()==null){
                dPayoff.setPersonPay(0.0);
            }
            dPayoff.setTotal(dPayoff.getMatiPay()+dPayoff.getPersonPay()+dPayoff.getPartPay());

            // 从  DService数据表中调取"StartTime","estimate"
            DService dService=dServiceMapper.selectByPrimaryKey(sId);
            if(dService!=null){
                dPayoff.setStartTime(dService.getStartTime());
                dPayoff.setEstimate(dService.getEstimate());
                dPayoff.setcName(dService.getcName());
            }
            try {
                dPayoffMapper.insertSelective(dPayoff);
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException();
            }

        }

    }


    @Override
    public DPayoff selectDPayoffListByServiceId(Integer serviceId){

        DPayoffExample dPayoffExample=new DPayoffExample();
        DPayoffExample.Criteria criteria=dPayoffExample.createCriteria();
        criteria.andServiceIdEqualTo(serviceId);
        List<DPayoff> dPayoffList=dPayoffMapper.selectByExample(dPayoffExample);
        return dPayoffList.get(0);
    }

    @Override
    public List<DPayoff> selectDPayoff(DPayoff dPayoff) {
        PageHelper.startPage(dPayoff.getPageNo(), dPayoff.getPageSize());
        DPayoffExample dPayoffExample=new DPayoffExample();
        DPayoffExample.Criteria criteria=dPayoffExample.createCriteria();
        if(dPayoff.getcName()!=null&&!"".equals(dPayoff.getcName())){
            criteria.andCNameEqualTo(dPayoff.getcName());
        }
        return dPayoffMapper.selectByExample(dPayoffExample);
    }
}
